<?php

/**
 * 前台 新闻 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: show.class.php 48 2011-07-18 09:57:45Z zwt007 $
 * http://www.t-mac.org；
 */
class user extends Model
{

    private $user_table;
    private $userstype_table;
    private $post_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->user_table = '#@__users';
        $this->userstype_table = '#@__users_type';
        $this->post_table = '#@__post';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addUser($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->user_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个管理员信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getuserInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->user_table WHERE uid = $id LIMIT 0,1");
        return $rs;
    }

    public function checkUserName($username, $id)
    {
        $sql = "SELECT * FROM $this->user_table WHERE username = '{$username}'";
        if ($id <> 0)
            $sql .= " AND uid <> $id";
        $sql .= " LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        if ($rs) {
            $this->redirect('名称重复啦!');
            exit;
        }
    }

    /**
     * 发布博客
     * @param <type> $info
     * @param <type> $mode
     * @param <type> $where
     * @return <type>
     */
    public function addPost($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->post_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取所有资讯
     * @param string $url
     * @param string $type
     * @return <type>
     */
    public function getPostList($url = null)
    {
        $uid = $_SESSION['uid'];
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->post_table WHERE uid='$uid'");
        if ($url == '')
            $url = PHP_SELF . '?m=user&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(12);
        $limit = $pages->getSqlLimit();
        $sql = "SELECT * FROM $this->post_table WHERE uid='$uid' ORDER BY orderby ASC, id DESC LIMIT $limit";
        $rs = $this->db->getAll($sql);
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['post_time'] = date('Y-m-d', $v['post_time']);
                $rs[$k]['edit_time'] = date('Y-m-d', $v['edit_time']);
            }
        }

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯! ";
        }

        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getPostInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->post_table WHERE id = '$id' LIMIT 0,1");
        return $rs;
    }

    public function checkUser($id)
    {
        $info = $this->getPostInfo($id);
        $uid = $_SESSION['uid'];
        if ($info['uid'] == $uid) {
            return true;
        } else {
            $this->redirect('请操作自己的博客！');
            exit();
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function delPost($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->post_table WHERE id in ($id)");
        return $rs;
    }



}